//
//  DingdanXiangqingViewController.swift
//  联付商圈
//
//  Created by zhangqi on 2017/8/28.
//  Copyright © 2017年 lianfu. All rights reserved.
//

import UIKit
import Alamofire

class DingdanXiangqingViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBAction func dis(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBOutlet weak var xqTableView: UITableView!
    
    var titleArr = ["","商品明细","订单详情",""]
    var tt = ["订单号:","创建时间:","支付方式:","订单状态:"]
    var tttt = ["","","",""]
    var spname = ""
    var zffasngshi = ""
    var cjshijian = ""
    var ddddh = ""
    var ddzt = ""
    var xqdic = NSArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        shanchudingdan(x:ddh)
        xqTableView.tableFooterView = UIView(frame:CGRect.zero)
        xqTableView.showsVerticalScrollIndicator = false
    }

    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated. ggg
    }
    

    func shanchudingdan(x:String){
        let par = ["ddh":"\(x)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/showdingdan?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: par).responseJSON {Request in
            switch Request.result {
            case .success:
                let x = JSON(data: Request.data!)
                self.spname = x["shop_name"].string!
                self.zffasngshi = x["mode"].string!
                self.cjshijian = x["created_at"].string!
                self.ddddh = x["$ddh"].string!
                let zt = x["state"].string!
                                if zt == "\(0)" {
                                    self.ddzt = "未支付"
                                } else if zt == "\(1)" {
                                    self.ddzt = "未使用"
                                } else if zt == "\(2)" {
                                    self.ddzt = "已使用"
                                }

                self.xqdic = x["dingdan"].arrayObject! as NSArray
                self.tttt[0] = self.ddddh
                self.tttt[1] = self.cjshijian
                self.tttt[2] = self.zffasngshi
                self.tttt[3] = self.ddzt
                self.xqTableView.reloadData()
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else if section == 1 {
                return xqdic.count
        } else {
            return 4
        }
//        } else {
//            return 1
//        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DdxqTitleTableViewCell", for: indexPath) as! DdxqTitleTableViewCell
            cell.titleLab.text = spname//(xqdic["shop_name"] as! String)
            return cell
        } else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DdxqShangpinTableViewCell", for: indexPath) as! DdxqShangpinTableViewCell
            let result = xqdic[indexPath.row] as! NSDictionary
            cell.sponeLab.text = (result["name"] as! String)
            cell.sptwoLab.text = "X" + "\(result["pnum"]!)"
            cell.spthreeLab.text = "¥" + "\(result["price"]!)"
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DdxqXinxiTableViewCell", for: indexPath) as! DdxqXinxiTableViewCell
            cell.xxonelab.text = tt[indexPath.row]
            cell.xxtwolab.text = tttt[indexPath.row]
            return cell
        }
//        else {
//            let cell = tableView.dequeueReusableCell(withIdentifier: "DdxqTitleTableViewCell", for: indexPath) as! DdxqTitleTableViewCell
//            cell.titleLab.text = "再来一单"
//            return cell
//        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        xqTableView.deselectRow(at: indexPath, animated: false)
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headTitle: UILabel = UILabel()
            headTitle.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)
            headTitle.text = (self.titleArr[section])
            headTitle.textColor = UIColor.gray
            headTitle.font = UIFont.systemFont(ofSize: 14)
            headTitle.textAlignment = NSTextAlignment.left
            return headTitle
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 20
        }
    

}
